package org.jeecg.modules.demo.LAPI.service.impl;

import org.jeecg.modules.demo.LAPI.entity.PersonnelPerson;
import org.jeecg.modules.demo.LAPI.entity.PersonnelRegion;
import org.jeecg.modules.demo.LAPI.entity.PersonnelIdentification;
import org.jeecg.modules.demo.LAPI.entity.PersonnelImage;
import org.jeecg.modules.demo.LAPI.mapper.PersonnelRegionMapper;
import org.jeecg.modules.demo.LAPI.mapper.PersonnelIdentificationMapper;
import org.jeecg.modules.demo.LAPI.mapper.PersonnelImageMapper;
import org.jeecg.modules.demo.LAPI.mapper.PersonnelPersonMapper;
import org.jeecg.modules.demo.LAPI.service.IPersonnelPersonService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 人员信息表
 * @Author: jeecg-boot
 * @Date:   2024-02-04
 * @Version: V1.0
 */
@Service
public class PersonnelPersonServiceImpl extends ServiceImpl<PersonnelPersonMapper, PersonnelPerson> implements IPersonnelPersonService {

	@Autowired
	private PersonnelPersonMapper personnelPersonMapper;
	@Autowired
	private PersonnelRegionMapper personnelRegionMapper;
	@Autowired
	private PersonnelIdentificationMapper personnelIdentificationMapper;
	@Autowired
	private PersonnelImageMapper personnelImageMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PersonnelPerson personnelPerson, List<PersonnelRegion> personnelRegionList,List<PersonnelIdentification> personnelIdentificationList,List<PersonnelImage> personnelImageList) {
		personnelPersonMapper.insert(personnelPerson);
		if(personnelRegionList!=null && personnelRegionList.size()>0) {
			for(PersonnelRegion entity:personnelRegionList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelRegionMapper.insert(entity);
			}
		}
		if(personnelIdentificationList!=null && personnelIdentificationList.size()>0) {
			for(PersonnelIdentification entity:personnelIdentificationList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelIdentificationMapper.insert(entity);
			}
		}
		if(personnelImageList!=null && personnelImageList.size()>0) {
			for(PersonnelImage entity:personnelImageList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelImageMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PersonnelPerson personnelPerson,List<PersonnelRegion> personnelRegionList,List<PersonnelIdentification> personnelIdentificationList,List<PersonnelImage> personnelImageList) {
		personnelPersonMapper.updateById(personnelPerson);
		
		//1.先删除子表数据
		personnelRegionMapper.deleteByMainId(personnelPerson.getId());
		personnelIdentificationMapper.deleteByMainId(personnelPerson.getId());
		personnelImageMapper.deleteByMainId(personnelPerson.getId());
		
		//2.子表数据重新插入
		if(personnelRegionList!=null && personnelRegionList.size()>0) {
			for(PersonnelRegion entity:personnelRegionList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelRegionMapper.insert(entity);
			}
		}
		if(personnelIdentificationList!=null && personnelIdentificationList.size()>0) {
			for(PersonnelIdentification entity:personnelIdentificationList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelIdentificationMapper.insert(entity);
			}
		}
		if(personnelImageList!=null && personnelImageList.size()>0) {
			for(PersonnelImage entity:personnelImageList) {
				//外键设置
				entity.setPersonId(personnelPerson.getId());
				personnelImageMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		personnelRegionMapper.deleteByMainId(id);
		personnelIdentificationMapper.deleteByMainId(id);
		personnelImageMapper.deleteByMainId(id);
		personnelPersonMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			personnelRegionMapper.deleteByMainId(id.toString());
			personnelIdentificationMapper.deleteByMainId(id.toString());
			personnelImageMapper.deleteByMainId(id.toString());
			personnelPersonMapper.deleteById(id);
		}
	}
	
}
